namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class RoleMenuBLL
    {
        public int Add(RoleMenu model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into RoleMenu(");
            builder.Append("RoleID,MenuCode");
            builder.Append(") values (");
            builder.Append("@RoleID,@MenuCode");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleID", model.RoleID), new SqlParameter("@MenuCode", model.MenuCode) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int AuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from RoleMenu ");
            builder.Append(" where AuthorityID=@AuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AuthorityID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(int RoleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" delete from RoleMenu ");
            builder.Append(" where RoleID =" + RoleID);
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<RoleMenu> GetList(int roleid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM RoleMenu ");
            builder.Append(" where RoleID= ").Append(roleid);
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            RoleMenu item = null;
            List<RoleMenu> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<RoleMenu>();
                foreach (DataRow row in table.Rows)
                {
                    item = new RoleMenu();
                    if (row["AuthorityID"].ToString() != "")
                    {
                        item.AuthorityID = int.Parse(row["AuthorityID"].ToString());
                    }
                    if (row["RoleID"].ToString() != "")
                    {
                        item.RoleID = int.Parse(row["RoleID"].ToString());
                    }
                    item.MenuCode = row["MenuCode"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public RoleMenu GetModel(int AuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select AuthorityID, RoleID, MenuCode  ");
            builder.Append("  from RoleMenu ");
            builder.Append(" where AuthorityID=@AuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AuthorityID;
            RoleMenu menu = new RoleMenu();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["AuthorityID"].ToString() != "")
                {
                    menu.AuthorityID = int.Parse(row["AuthorityID"].ToString());
                }
                if (row["RoleID"].ToString() != "")
                {
                    menu.RoleID = int.Parse(row["RoleID"].ToString());
                }
                menu.MenuCode = row["MenuCode"].ToString();
                return menu;
            }
            return null;
        }

        public bool Update(RoleMenu model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update RoleMenu set ");
            builder.Append(" RoleID = @RoleID , ");
            builder.Append(" MenuCode = @MenuCode  ");
            builder.Append(" where AuthorityID=@AuthorityID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AuthorityID", model.AuthorityID), new SqlParameter("@RoleID", model.RoleID), new SqlParameter("@MenuCode", model.MenuCode) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

